<template>
    <div class="facilitator_rate">
        <viewTitle :title="'费率设置'" :border="true"> </viewTitle>
        <div class="rate_h">
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span>政府补贴费率设置：
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.government_subsidy}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'government_subsidy')" ref="government_subsidy" v-model="detail.government_subsidy"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span> 增值税率：
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.educational_tariff}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值"  @change="judgeOne($event, 'educational_tariff')"  ref="educational_tariff"  v-model="detail.educational_tariff"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span>个人所得税率:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.personal_tax_rate}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值"  @change="judgeOne($event, 'personal_tax_rate')"  ref="personal_tax_rate" v-model="detail.personal_tax_rate"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span>地方附加税率:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.local_surtax_rate}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'local_surtax_rate')"  ref="local_surtax_rate" v-model="detail.local_surtax_rate"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span> 技术服务费率:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.tech_service_rate}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder=" 数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'tech_service_rate')"  ref="tech_service_rate" v-model="detail.tech_service_rate"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span> 差额税率:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.diff_rate}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder=" 数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'diff_rate')"  ref="diff_rate" v-model="detail.diff_rate"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span>平台代理费用（差额）:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.plat_agent_rate_diff}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'plat_agent_rate_diff')"  ref="plat_agent_rate_diff" v-model="detail.plat_agent_rate_diff"></el-input>
                    </div>
                </div>
            </div>
            <div class="rate_block">
                <div class="rate_block_title">
                    <span v-if="type > 0">*</span>平台代理费率（增值）:
                </div>
                <div class="rate_block_input">
                    <div v-if="type === 0"> {{detail.plat_agent_rate_edu}} </div>
                    <div v-else>
                        <el-input type="text" clearable placeholder="数值1表示100%，请填写小于1的费率值" @change="judgeOne($event, 'plat_agent_rate_edu')"  ref="plat_agent_rate_edu" v-model="detail.plat_agent_rate_edu"></el-input>
                    </div>
                </div>
            </div>
        </div>

        <!-- 新增、编辑的时候还有下面的按钮，用于提交数据 -->
        <!-- 新增 -->
        <el-row style="margin:30px 0px 20px 30px;" v-if="type > 0">
            <el-button type="primary" @click="submitRate" v-if="type == 1">下一步</el-button>
            <!-- 编辑 -->
            <el-button type="primary" @click="submitRate" v-if="type == 2">保存</el-button>
        </el-row>
    </div>
</template>

<script>
    export default {
        name: "FacilitatorRate",
        props:{
            type:{              //编辑类型：0：查看、1：新增、2：编辑
                type: Number,
                default: 1
            },
            facilitator_id_add:0,       //新增时，传过来的服务商ID
        },
      data(){
        return  {
            facilitator_id:0,                   //服务商ID
            detail:{                            //基本信息资料
                government_subsidy: '',            //政府补贴费率
                educational_tariff: '',         //增值税率
                personal_tax_rate: '',          //个人所得税率
                local_surtax_rate: '',           //地方附加税率
                tech_service_rate : '',         //技术服务费率
                diff_rate : '',                 //差额税率
                plat_agent_rate_edu : '',      //平台代理费率（增值）
                plat_agent_rate_diff : '',      //平台代理费率（差额）
            }
        }
        
    },
    watch:{
        facilitator_id_add(newVal, oldVal){
            this.facilitator_id = newVal;
        }
    },

    mounted() {
        if(parseInt(this.$route.query.facilitator_id) > 0){
            this.facilitator_id = parseInt(this.$route.query.facilitator_id);
            this.facilitatorRate();
        }
    },
    methods:{
        facilitatorRate(){
            this.$raxios.post(this.$mapapi.facilitatorRateDetail, {data:{facilitator_id: this.facilitator_id}}).then(res => {
                if(res.code != 200)     return  Promise.reject(res.msg);
                if(!(res.data.length > 0)){
                    this.detail = res.data;
                }
            }).catch(res => {
                this.$message.error(res);
            });
        },
        submitRate(){
            this.detail.facilitator_id = this.facilitator_id;
            this.$raxios.post(this.$mapapi.saveFacilitatorRate, {data: this.detail}).then(res => {
                if(res.code != 200){
                    return  Promise.reject(res.msg);
                }
                if(this.type == 1){         //新增的话，还需要向父级发送消息，再进行下一步
                    this.$emit('submitRate');
                }else{              //编辑的话，这里直接弹出成功信息即可
                    this.$message.success(res.msg);
                    let that = this;
                    setTimeout(function() {
                        that.$blackRoute({ path: '/facilitator_list',query:{},that:this});                 // 返回指定路由-删除标题栏
                    }, 500);
                }
            }).catch(res => {
                this.$message.error(res);
                
            });
        },

        //判断数值小于1
        judgeOne(e, refere){
            console.log('judge-one', e);
            if(parseInt(e) >= 1){        //如果填写的费率数值大于等于1的话，那么就提醒报错，并且删除原来的数值
                this.$message.error('费率数值必须小于1');
                this.$refs[refere].focus();
                this.detail[refere] = '';
            }
        }

    }
    

    }
</script>

<style  lang="less" :scope="true" src="@/style/facilitator/facilitator_list/facilitator_rate.less">

</style>